<template>
  <div>
    <h1>欢迎来到酷鲨停车场</h1>
    <img src="../assets/imgs/shark.png">
    <h2>CoolSharkParking</h2>
    <el-card style="width: 600px;height: 500px;margin: 0 auto;
    background-color: rgba(255,255,255,0.3)">

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"
               style="position: relative;right: 20px">
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="ruleForm.realName"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="ruleForm.nickname"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="身份证" prop="cardId">
          <el-input v-model="ruleForm.cardId"></el-input>
        </el-form-item>
        <el-form-item label="车牌" prop="carId">
          <el-input v-model="ruleForm.carId"></el-input>
        </el-form-item>
        <el-form-item style="position: relative;right: 30px">

          <el-button @click="resetForm('ruleForm')">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<style>
body {
  text-align: center;
  background-image: url("../assets/imgs/bg.jpg");
  margin: 0; /*去掉自带外边距*/
  background-size: cover; /*cove  把背景图片设置为封面  设置之后  背景图片 不会随着浏览器缩放而缩放 会自动填充 */
  background-repeat: no-repeat; /*背景图片不重复*/
}

h1 {
  font-size: 72px;
  color: #0096dc;
  margin-bottom: 0;
}

h2 {
  font-size: 32px;
  color: #0096dc;
  margin-bottom: 0;
}
</style>
<script>
export default {
  data() {
    return {
      ruleForm: {
        realName:'',
        nickname: '',
        username: '',
        password: '',
        phone: '',
        cardId:'',
        carId: '',
      },
      rules: {
        realName: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
          {min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur'}
        ],
        nickname: [
          {required: true, message: '请输入昵称', trigger: 'blur'},
          {min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur'}
        ],
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入电话号码', trigger: 'blur'},
          {min: 11, max: 11, message: '请输入十一位电话号码', trigger: 'blur'}
        ],
        cardId: [
          {required: true, message: '输入身份证', trigger: 'blur'},
          {min: 18, max: 18, message: '输入有效身份证', trigger: 'blur'}
        ],
      }

    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('请求参数格式验证通过!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].validate((valid)=>{
        let url = 'http://localhost:8080/user/reg';
        console.log(this.ruleForm)
        this.axios.post(url,this.ruleForm).then((response) => {
          let responseBody = response.data;
          if (responseBody.code = 200) {
            this.$message({
              showClose: true,
              message: '注册成功！',
              type: 'success'
            });
            this.$router.push('/');
          } else if (responseBody.code == 400) {
            this.$message({
              showClose: true,
              message: responseBody.message,
              type: 'error'
            });
          }
        })
      });
    }

  }
}

</script>
